package arkanoid.components.ball;

import arkanoid.components.Palette;

import com.uqbar.vainilla.DeltaState;
import com.uqbar.vainilla.colissions.CollisionDetector;

public abstract class DisattachedBall extends BallState {
	
	private static int GAME_HEIGHT;
	private static int GAME_WIDTH;

	@Override
	public void update(Ball ball, DeltaState deltaState) {
		GAME_WIDTH  = ball.getGame().getDisplayWidth();
		GAME_HEIGHT = ball.getGame().getDisplayHeight();
		
		double advanced = deltaState.getDelta() * ball.getSpeed();
		ball.move(ball.getVersorI() * advanced, ball.getVersorJ() * advanced);
		
		//Si sale por abajo
		if (ball.getY() > GAME_WIDTH - ball.getDiameter()) {
			ball.getScene().removeBall(ball);
		}
		
		//Si sale por la derecha
		if (ball.getX() > GAME_HEIGHT - ball.getDiameter()) {
			ball.setX(GAME_HEIGHT - ball.getDiameter());
			ball.setVersorI(-ball.getVersorI());
		}
		
		//Si sale por arriba
		if (ball.getY() < 0) {
			ball.setY(0);
			ball.setVersorJ(-ball.getVersorJ());
		}
		
		//Si sale por la izquierda
		if (ball.getX() < 0) {
			ball.setX(0);
			ball.setVersorI(-ball.getVersorI());
		}
		
		//TODO: Implementar esto asi el codigo en las subclases queda más limpio.
//		Palette palette = ball.getScene().getPalette();
//		if (this.ballCollidesAgainstPalette(ball, palette)) {
//			this.performPaletteCollisionAction(ball, palette, deltaState);
//		}
	}
	
	protected boolean ballCollidesAgainstPalette(Ball ball, Palette palette) {
		return CollisionDetector.INSTANCE.collidesCircleAgainstRect(
				ball.getX(), ball.getY(), ball.getRatio(),
				palette.getX(), palette.getY(), palette.getWidth(), palette.getHeight()
				);
	}

}
